﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using bdxyit.com.RecruitmentAppSys.IDAL;
using bdxyit.com.RecruitmentAppSys.Models;
using bdxyit.com.RecruitmentAppSys.DBUtility;

/***
*机器名：BDXYIT-HUI
*项目名称：$projectname$
*命名空间：bdxyit.com.RecruitmentAppSys.SQLDAL
*文件名：AreaListModels
*系统时间：3/25/2012 3:32:50 PM
*当前用户：huihui
*创建时间：2012
*
*修改时间：2012 3/25/2012 3:32:50 PM
*修改事件：
***/

namespace bdxyit.com.RecruitmentAppSys.SQLDAL
{
    public class AreaListDAL : IAreaListDAL
    {
        private const string SQL_SELECT_AREALIST = " select AreaID,AreaName from AreaLists ";
        private const string SQL_SELECT_AREALIST_BY_AREAID = " select  top 1 AreaID,AreaName from AreaLists "
                                                             + "  where  AreaID=@AreaID";
        private const string SQL_DELETE_AREALIST_BY_AREAID = " delete from AreaLists "
                                                            + "  where  AreaID=@AreaID";
        private const string SQL_UPDATE_AREALIST_BY_AREAID = " update AreaLists set "
                                                            + " AreaID=@AreaID,"
                                                            + " AreaName=@AreaName"
                                                            + "  where  AreaID=@AreaID";
        private const string SQL_INSERT_AREALIST = " insert into AreaLists("
                                                + " AreaID,AreaName)"
                                                + "  values ("
                                                + " @AreaID,@AreaName)";
        private const string FIELD_AreaID = "@AreaID";
        private const string FIELD_AreaName = "@AreaName";
        public bool InsertAreaList(AreaList cmodel)
        {
            bool flag = false;
            SqlParameter[] AreaListParms = GetAreaListParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            AreaListParms[0].Value = cmodel.AreaID;
            AreaListParms[1].Value = cmodel.AreaName;

            foreach (SqlParameter parm in AreaListParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_INSERT_AREALIST;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool DeleteAreaList(string AreaID)
        {
            bool flag = false;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_AreaID, SqlDbType.VarChar, 10);
            parm.Value = AreaID;
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_DELETE_AREALIST_BY_AREAID;
                cmd.Parameters.Add(parm);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool UpdateAreaList(AreaList cmodel)
        {
            bool flag = false;
            SqlParameter[] AreaListParms = GetAreaListParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            AreaListParms[0].Value = cmodel.AreaID;
            AreaListParms[1].Value = cmodel.AreaName;

            foreach (SqlParameter parm in AreaListParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_UPDATE_AREALIST_BY_AREAID;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public AreaList SelectAreaListByAreaID(string AreaID)
        {
            AreaList cmodel = null;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_AreaID, SqlDbType.VarChar, 10);
            parm.Value = AreaID;
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_AREALIST_BY_AREAID, parm))
            {
                if (dr.Read())
                {
                    cmodel = new AreaList();
                    if (dr.HasRows == true)
                    {
                        if (dr["AreaID"] != null && dr["AreaID"].ToString() != "")
                        {
                            cmodel.AreaID = dr["AreaID"].ToString();
                        }
                        if (dr["AreaName"] != null && dr["AreaName"].ToString() != "")
                        {
                            cmodel.AreaName = dr["AreaName"].ToString();
                        }
                    }
                }
            }
            return cmodel;
        }
        public List<AreaList> SelectAreaList()
        {
            List<AreaList> selectlist = new List<AreaList>();
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_AREALIST))
            {
                while (dr.Read())
                {
                    AreaList cmodel = new AreaList();
                    //写添赋值语句
                    if (dr["AreaID"] != null && dr["AreaID"].ToString() != "")
                    {
                        cmodel.AreaID = dr["AreaID"].ToString();
                    }
                    if (dr["AreaName"] != null && dr["AreaName"].ToString() != "")
                    {
                        cmodel.AreaName = dr["AreaName"].ToString();
                    }

                    selectlist.Add(cmodel);
                }
            }
            return selectlist;
        }
        private SqlParameter[] GetAreaListParameters()
        {
            SqlParameter[] parms = new SqlParameter[] {
			    	new SqlParameter(FIELD_AreaID, SqlDbType.VarChar,10),
					new SqlParameter(FIELD_AreaName, SqlDbType.VarChar,20)};
            return parms;
        }

    }
}